package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.SubCategoria;
import edu.upc.clase.demo.service.SubCategoriaService;
import edu.upc.clase.demo.service.CategoriaService;
import edu.upc.clase.demo.service.EstadoService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Carlos
 */
@Controller("SubCategoriaController")
public class SubCategoriaController {
    
     @Autowired
    private SubCategoriaService subCategoriaService;
    
    @Autowired
    private CategoriaService categoriaService;
      
    @Autowired
    private EstadoService estadoService;
    
    private static Logger log = LoggerFactory.getLogger(SubCategoriaController.class);
    
    
    @RequestMapping("/SubCategoria/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("SubCategoria/index");
        List<SubCategoria> SubCategorias = subCategoriaService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("subCategoria", SubCategorias);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
       
    @RequestMapping(value = "/SubCategoria/new", method = RequestMethod.GET)
    public ModelAndView newSubCategoria() {
       
        ModelAndView mav = new ModelAndView("SubCategoria/new");
        SubCategoria subCategoria = new SubCategoria();       
        mav.getModelMap().put("subCategoria", subCategoria);
        mav.addObject("estado", estadoService.buscarCodigo("01"));
        mav.addObject("categoria", categoriaService.buscarTodos());
                  
        return mav;
     }
    

    @RequestMapping(value = "/SubCategoria/new", method = RequestMethod.POST)
    public String createSubCategoria(@ModelAttribute("subCategoria")SubCategoria subCategoria, SessionStatus status) {    
        subCategoriaService.insertar(subCategoria);
        status.setComplete();
        return "redirect:/pages/SubCategoria/index";
        
    }
    
    @RequestMapping(value = "/SubCategoria/edit", method = RequestMethod.GET)
    public ModelAndView editsubCategoria(@RequestParam("idSubCategoria")Integer idSubCategoria) {    
        ModelAndView mav = new ModelAndView("SubCategoria/edit");
        SubCategoria SubCategoria = subCategoriaService.buscar(idSubCategoria);
        mav.getModelMap().put("subCategoria", SubCategoria);
        mav.addObject("estado", estadoService.buscarCodigo("01"));
        mav.addObject("categoria", categoriaService.buscarTodos());
        return mav;        
    }
    
    @RequestMapping(value="/SubCategoria/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("subCategoria") SubCategoria subCategoria, SessionStatus status) {
        subCategoriaService.actualizar(subCategoria);
        status.setComplete();
        return "redirect:/pages/SubCategoria/index";
    }
    
    @RequestMapping("/SubCategoria/delete")
    public ModelAndView delete(@RequestParam("idSubCategoria")Integer idSubCategoria)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/SubCategoria/index");
        SubCategoria SubCategoria = subCategoriaService.buscar(idSubCategoria);
        subCategoriaService.eliminar(SubCategoria);
        return mav;
    }
     
    
    @RequestMapping(value = "/SubCategoria/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("SubCategoria/resultadoBusqueda");
        log.info("subCategoria = " + subCategoriaService.buscarPorNombre(criterio.getNombre()).size());
        mav.getModel().put("subCategoria",subCategoriaService.buscarPorNombre(criterio.getNombre()));
        return mav;
    }
    
}
